class EventEmitter {
    constructor() {
        this.events = {};
    }

    on(type, fn) { // 订阅
       if (this.events[type]) {
           this.events[type].push(fn);
       } else {
           this.events[type] = [fn];
       }
    }
    emit(type) { // 发布(触发所有订阅事件)
        if (!this.events[type]) return;
        this.events[type].forEach(fn => {
            fn();
        })
    }
    off(type, fn) { // 取消订阅
        if (!this.events[type]) return;
        if (!fn) {
            this.events[type] = null;
        } else {
            this.events[type] = this.events[type].filter(item => item !== fn);
        }
    }
}

let emitter = new EventEmitter();

emitter.on('have', () => {
    console.log('买买买111');
})

const fn = () => {
    console.log('买买买222');
}

emitter.on('have', fn)

emitter.off('have', fn);

emitter.emit('have');
